

import xadmin
from import_export import resources
from cost_query.models import CostQuery
from xadmin import views
from  xadmin import views
from xadmin.layout import Main,Fieldset,Side
from django.apps import apps

class CostQueryResource(resources.ModelResource):
    # class Meta:
    #     model = CostQuery

    # def __init__(self):
    #     super(CostQueryResource, self).__init__()
    #     field_list = apps.get_model('cost_query', 'CostQuery')._meta.fields
    #     self.verbose_name_dict = {}
    #     # 获取所有字段的verbose_name并存放在verbose_name_dict字典里
    #     for i in field_list:
    #         self.verbose_name_dict[i.name] = i.verbose_name
    # def get_export_fields(self):
    #     fields = self.get_fields()
    #     # 默认导入导出field的column_name为字段的名称
    #     # 这里修改为字段的verbose_name
    #     for field in fields:
    #         field_name = self.get_field_name(field)
    #         if field_name in self.verbose_name_dict.keys():
    #             field.column_name = self.verbose_name_dict[field_name]
    #             # 如果设置过verbose_name，则将column_name替换为verbose_name
    #             # 否则维持原有的字段名
    #     return fields
    class Meta:
        model = CostQuery
        skip_unchanged = True
        report_skipped = True
        import_id_fields = ('id',)
        fields = (
            'id',
            'name',
            'grade',
            'the_class',
            'id_number',
            'pay_time',
            'pay_type',
            # 'pay_lines',
            # 'pay_times',
            # 'pay_status'
        )
        exclude = (
            'create_time',
            'update_time',
        )
"""
 def __init__(self):
        super(ProductInfoResource, self).__init__()
        field_list = apps.get_model('product', 'ProductInfo')._meta.fields
        # 应用名与模型名
        self.verbose_name_dict = {}
        # 获取所有字段的verbose_name并存放在verbose_name_dict字典里
        for i in field_list:
            self.verbose_name_dict[i.name] = i.verbose_name

    def get_export_fields(self):
        fields = self.get_fields()
        # 默认导入导出field的column_name为字段的名称
        # 这里修改为字段的verbose_name
        for field in fields:
            field_name = self.get_field_name(field)
            if field_name in self.verbose_name_dict.keys():
                field.column_name = self.verbose_name_dict[field_name]
                # 如果设置过verbose_name，则将column_name替换为verbose_name
                # 否则维持原有的字段名
        return fields

    class Meta:
        model = ProductInfo

        skip_unchanged = True
        # 导入数据时，如果该条数据未修改过，则会忽略
        report_skipped = True
        # 在导入预览页面中显示跳过的记录

        import_id_fields = ('id',)
        # 对象标识的默认字段是id，您可以选择在导入时设置哪些字段用作id

        fields = (
            'id',
            'product_name',
            'product_picture',
            'product_describe',
            'product_manager',
        )
        # 白名单

        exclude = (
            'product_detail',
            'create_time',
            'update_time',
        )
"""
import_excel = False #控制开关
class CostQueryAdmin():

    list_display = ['name', 'grade','the_class','id_number','pay_time','pay_type','pay_lines','pay_times','pay_status']
    search_fields = ['name','grade','the_class','id_number','pay_time','pay_type','pay_lines','pay_times','pay_status']   #搜索的字段（所有字段一起搜索）
    list_filter =  ['the_class','grade','pay_time','pay_type','pay_status']
    list_per_page = 20
    import_export_args = {'import_resource_class': CostQueryResource,}

# xadmin.site.register(CostQuery, CostQueryAdmin)




